#pragma once

#include <iostream>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

namespace NetSockModule
{
    enum
    {
        Socket_Err = 1,
        Bind_Err,
        Usage_Err,
        Listen_Err,
        Connect_Err,
        Accept_Err
    };

#define CONVE(ptr) ((struct sockaddr *)(ptr))
    static const int defualtsockfd = -1;

    class Socket
    {
    public:
        virtual void CreateSocketOrDie() = 0;
        virtual void BindSocketOrDie(uint16_t port) = 0;
        virtual void ListenSocketOrDie(int backlog) = 0;
        virtual int AcceptSocketOrDie(std::string *peerip, uint16_t *peerport) = 0;
        virtual bool ConnectSocketOrDie(std::string &serverip, uint16_t serverport) = 0;
        virtual int GetSockFd() = 0;
        virtual void SetSockFd(int SockFd) = 0;
        virtual void SockFdClose() = 0;
        virtual bool Recv(std::string *out, int size) = 0;
        virtual bool Send(std::string &in) = 0;

    public:
        void BuildListenSocketMethod(int port, int backlog)
        {
            CreateSocketOrDie();
            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }

        bool BuildConnectSocketMethod(std::string &serverip, uint16_t serverport)
        {
            CreateSocketOrDie();
            return ConnectSocketOrDie(serverip, serverport);
        }

        void BuildNormalSocketMethod(int sockfd)
        {
            SetSockFd(sockfd);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int sockfd = defualtsockfd)
            : _sockfd(sockfd)
        {
        }
        ~TcpSocket()
        {
        }
        void CreateSocketOrDie() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                exit(Socket_Err);
            }
        }
        void BindSocketOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;
            if (::bind(_sockfd, CONVE(&local), sizeof(local)) < 0)
                exit(Bind_Err);
        }
        void ListenSocketOrDie(int backlog) override
        {
            if (listen(_sockfd, backlog) < 0)
                exit(Listen_Err);
        }
        int AcceptSocketOrDie(std::string *peerip, uint16_t *peerport) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newsockfd = accept(_sockfd, CONVE(&peer), &len);
            if (newsockfd < 0)
            {
                return -1;
            }
            // 把数据带出去
            *peerip = inet_ntoa(peer.sin_addr);
            *peerport = ntohs(peer.sin_port);
            return newsockfd;
        }
        bool ConnectSocketOrDie(std::string &serverip, uint16_t serverport) override
        {
            struct sockaddr_in server;
            server.sin_family = AF_INET;
            server.sin_port = htons(serverport);
            server.sin_addr.s_addr = inet_addr(serverip.c_str());
            socklen_t len = sizeof(server);
            if (connect(_sockfd, CONVE(&server), len) < 0)
            {
                return false;
                exit(Connect_Err);
            }
            return true;
        }
        int GetSockFd() override
        {
            return _sockfd;
        }
        void SetSockFd(int SockFd) override
        {
            _sockfd = SockFd;
        }
        void SockFdClose() override
        {
            if (_sockfd >= 0)
                close(_sockfd);
        }
        bool Recv(std::string *out, int size) override
        {
            char outbuffer[size];
            ssize_t n = recv(_sockfd, outbuffer, sizeof(outbuffer) - 1, 0);
            if (n > 0)
            {
                outbuffer[n] = 0;
                *out += outbuffer; // 这故意拼接
                return true;
            }
            else if (n == 0 || n < 0)
            {
                return false;
            }
            return true;
        }
        bool Send(std::string &in) override
        {
            ssize_t n = send(_sockfd, in.c_str(), in.size(), 0);
            if (n < 0)
                return false;
            return true;
        }

    private:
        int _sockfd;
    };
}